<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>提货点对账</title>
    <% include("/common/common_css_js.html"){} %>
</head>
<body>
<div style="padding-bottom: 5px;">
    <div id="queryForm" class="uc-toolbaroverflow" _borderStyle="border-top:0;">
        <span>分单号:</span><input id="keyBillCode" name="keyBillCode" emptyText="请输入分单号"  class="mini-textbox" selectOnFocus="true" required="false" requiredErrorText="分单号不能为空" />
        <span class="separator"></span>
        <span>分单归属部门:</span>
        <input id="keyAttributionDepartmentId" name="keyAttributionDepartmentId" class="mini-treeselect" style="width:250px;" textField="name" valueField="id" emptyText="请选择..."
               expandOnLoad="2"  virtualScroll="true"  multiSelect = "true" showFolderCheckBox="true"
               required="false" allowInput="true" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择归属部门"/>
        <span class="separator"></span>
        <span>运输种类:</span><input id="keyTransportSpeciesId" name="keyTransportSpeciesId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                                 required="false" allowInput="false" showNullItem="false" nullItemText="请选择..." requiredErrorText="没有选择运输种类" onvaluechanged="transportSpeciesIdChange" />
        <span class="separator"></span>
        <span>承运方式:</span><input id="keyTransportWayId" name="keyTransportWayId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                                 required="false" allowInput="false" showNullItem="false" nullItemText="请选择..." requiredErrorText="没有选择承运方式"/>


        <div style="margin-top: 5px"></div>

        <span>提货点:</span>
        <div id="keyPickUpPointId" name="keyPickUpPointId" class="mini-autocomplete" style="width:350px;"  popupWidth="500" textField="name" valueField="id"
             required="false" requiredErrorText="提货点不能为空"
             url="${base}/pages/basic_data_manage/xt_pick_up_point/queryPickUpPointByCondition?cityId="
             searchField="key"
             emptyText="提货点名称">
            <div property="columns">
                <div header="提货点名称" field="name" width="150"></div>
                <div header="提货点电话" field="phone" width="150"></div>
                <div header="提货点地址" field="address" width="200"></div>
            </div>
        </div>
        <span class="separator"></span>
        <span>提货方式:</span>
        <input id="keyPickUpGoodsWayId" name="keyPickUpGoodsWayId" class="mini-combobox" style="width:120px;" textField="name" valueField="id" emptyText="请选择..."
               required="false" allowInput="false" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择提货方式"/>
        <span class="separator"></span>
        <span>开单日期:</span>
        <input id="keyOpenBillStartDate" name="keyOpenBillStartDate"  class="mini-datepicker"   format="yyyy-MM-dd"/>--<input id="keyOpenBillEndDate" name="keyOpenBillEndDate" class="mini-datepicker"  format="yyyy-MM-dd"/>

        <div style="margin-top: 5px"></div>
        <span>发货人付款方式:</span>
        <input id="keyDeliveryCustomerPaymentMethodId" name="keyDdeliveryCustomerPaymentMethodId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
               required="false" allowInput="false" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择发货人付款方式" />
        <span class="separator"></span>
        <span>收货人付款方式:</span>
        <input id="keyReceiveCustomerPaymentMethodId" name="keyReceiveCustomerPaymentMethodId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
               required="false" allowInput="false" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择收货人付款方式" />



        <div style="margin-top: 5px"></div>


        <% if(so.hasPermission("bNativeBillCwPickUp:query") || so.hasPermission("cwNativeBill:query")){%>
        <a class="mini-button" iconCls="icon-search" onclick="searchBill()">查询</a>
        <% } %>

        <% if(so.hasPermission("bNativeBillCwPickUp:queryCndClear") || so.hasPermission("cwNativeBill:queryCndClear")){%>
        <a class="mini-button" iconCls="icon-new" onclick="clearSearch()">清空查询条件</a>
        <% } %>

        <% if(so.hasPermission("bNativeBillCwPickUp:confirm")){%>
        <span class="separator"></span>
        <a class="mini-button" iconCls="icon-lock" onclick="accountStatusChange(432)">确认对账</a>
        <% } %>

        <% if(so.hasPermission("bNativeBillCwPickUp:cancel")){%>
        <span class="separator"></span>
        <a class="mini-button" iconCls="icon-unlock" onclick="accountStatusChange(431)">取消对账</a>
        <% } %>

        <% if(so.hasPermission("bNativeBillCwPickUp:download")){%>
        <span class="separator"></span>
        <#download_grid id="downloadGrid" theGridId="datagrid1" theFileName="提货点对账表"/>
        <% } %>

    </div>
</div>

<div class = "mini-fit">
    <div id="datagrid1" class="mini-datagrid"
         style="width: 100%; height: 100%;"
         url="${base}/pages/native_bill_manage/queryBill" idField="id"
         showPager="true" pageSize="20" sizeList="[10,20,30,50,100]"
         allowResize="true"
         multiSelect = "true"
         showGroupsSummary="true" showSummaryRow="true" summaryPosition="bottom"
         allowSortColumn = "true"
         sortField="createTime"
         sortOrder="desc"
         ondrawsummarycell="summaryRight" >
        <div property="columns">
            <div type="checkcolumn" ></div>
            <div field="accountCheckStatusId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"sysDic"}' renderer="commonRender">对账状态</div>
            <div field="openBillDate" width="140" headerAlign="center" align="center" allowSort="false" dateFormat="yyyy-MM-dd HH:mm:ss" renderer="ondayRenderer">开单日期</div>
            <div field="id" width="140" headerAlign="center" align="center" allowSort="true" renderer="showNativeBillRender">分单号</div>
            <div field="billLoadMasterBillIds" width="140" headerAlign="center" align="left" allowSort="true">主单号</div>
            <div field="flightName" width="140" headerAlign="center" align="center" allowSort="true">班次号</div>
            <div field="airportId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"airport"}' renderer="commonRender">始发机场</div>
            <div field="destCityId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"city"}' renderer="commonRender">目的市</div>
            <div field="pickUpPointName" width="140" headerAlign="center" align="center" allowSort="true">提货点名称</div>
            <div field="pickUpPointPhone" width="140" headerAlign="center" align="center" allowSort="true" renderer="commonDesensitization">提货点电话</div>
            <div field="goodsName" width="140" headerAlign="center" align="center" allowSort="true" >货物名称</div>
            <div field="totalNumber" width="140" headerAlign="center" align="right" allowSort="false" summaryType="sum">总件数</div>
            <div field="actualWeight" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">实际重量</div>
            <div field="chargeableWeight" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">计费重量</div>
            <div field="weightRate" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer">重量费率</div>
            <div field="agentDeliveryCharge" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">代理送货费</div>
            <div field="deliveryCustomerName" width="140" headerAlign="center" align="center" allowSort="true">发货人名称</div>
            <div field="receiveCustomerName" width="140" headerAlign="center" align="center" allowSort="true">收货人名称</div>
            <div field="receiveCustomerAddress" width="140" headerAlign="center" align="center" allowSort="true">收货人地址</div>
            <div field="deliveryCustomerPaymentMethodId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"sysDic"}' renderer="commonRender">发货人付款方式</div>
            <div field="deliveryCustomerPaymentAmount" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">发货人付款金额</div>
            <div field="receiveCustomerPaymentMethodId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"sysDic"}' renderer="commonRender">收货人付款方式</div>
            <div field="receiveCustomerPaymentAmount" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">收货人付款金额</div>

        </div>
    </div>
</div>

<script type="text/javascript">

    mini.parse();

    let keyBillCode = mini.get("keyBillCode");
    let keyAttributionDepartmentId = mini.get("keyAttributionDepartmentId");
    let keyTransportSpeciesId = mini.get("keyTransportSpeciesId");
    let keyTransportWayId = mini.get("keyTransportWayId");

    let keyPickUpPointId = mini.get("keyPickUpPointId");
    let keyPickUpGoodsWayId = mini.get("keyPickUpGoodsWayId");
    let keyOpenBillStartDate = mini.get("keyOpenBillStartDate");
    let keyOpenBillEndDate = mini.get("keyOpenBillEndDate");
    let keyDeliveryCustomerPaymentMethodId = mini.get("keyDeliveryCustomerPaymentMethodId");
    let keyReceiveCustomerPaymentMethodId = mini.get("keyReceiveCustomerPaymentMethodId");



    let grid = mini.get("datagrid1");

    $(function () {

        let keyAttributionDepartmentIdJsonData = commonRenderDropList("department");
        keyAttributionDepartmentId.loadList(keyAttributionDepartmentIdJsonData,"id","pid");

        // 运输种类下拉列表
        let transportSpeciesIdJsonData=commonRenderSysDicDropListByKey("sysDic","XT_DIC_TRANSPORT_SPECIES");
        keyTransportSpeciesId.load(transportSpeciesIdJsonData);

        // 承运方式下拉列表
        let transportWayIdJsonData=commonRenderSysDicDropListByKey("sysDic","XT_DIC_TRANSPORT_WAY");
        keyTransportWayId.load(transportWayIdJsonData);

        // 提货方式下拉列表
        let pickUpGoodsWayIdJsonData=commonRenderSysDicDropListByKey("sysDic","XT_DIC_PICK_UP_GOODS_WAY");
        keyPickUpGoodsWayId.load(pickUpGoodsWayIdJsonData);

        // 发货人付款方式
        let keyDeliveryCustomerPaymentMethodIdJsonData = commonRenderSysDicDropListByKey("sysDic","XT_DIC_DELIVERY_CUSTOMER_PAYMENT_METHOD");
        keyDeliveryCustomerPaymentMethodId.load(keyDeliveryCustomerPaymentMethodIdJsonData);

        // 收货人付款方式
        let keyReceiveCustomerPaymentMethodIdJsonData = commonRenderSysDicDropListByKey("sysDic","XT_DIC_RECEIVE_CUSTOMER_PAYMENT_METHOD");
        keyReceiveCustomerPaymentMethodId.load(keyReceiveCustomerPaymentMethodIdJsonData);

        clearSearch()

        // 加载列表数据
        grid.load();
    });

    function clearSearch(){
        let form = new mini.Form("#queryForm");
        form.clear();
    }

    let cancelStatus = {notCanceled:411, canceled:412}

    /**
     * 根据查询条件查询分单
     * 410,0,作废状态,作废状态,XT_DIC_CANCEL_STATUS
     411,410,未作废,未作废,
     412,410,已作废,已作废,
     */

    function searchBill(){
        let form = new mini.Form("#queryForm");
        let data = form.getData(true);
        // 一定是未作废的单子
        data.keyCancelStatusId = cancelStatus.notCanceled;
        grid.load(data);
    }

    function showNativeBillRender(e){
        let nativeBillId = e.value;
        let htmlStr = "<a href='javascript:void(0);' onclick='showViewNativeBill(\""+ nativeBillId +"\")'>" + nativeBillId + "</a>";
        return htmlStr;
    }

    function showViewNativeBill(nativeBillId){
        let tabUrl="${base}/pages/native_bill_manage/showUpdateBill?opType=view&id=" + nativeBillId;
        parent.window.addTabToMainPage("分单查看","分单查看",tabUrl);
    }


    /**
     *
     * 350,0,分单配载状态,分单配载状态,XT_DIC_BILL_LOAD_STATUS
     * NO_LOAD(351, "未配载", "未配载"),
     * PART_LOAD(352, "部分配载", "部分配载"),
     * ALL_LOAD(353, "全部配载", "全部配载");

     * 320,0,结算状态,结算状态,XT_DIC_SETTLEMENT_STATUS
     * 321,320,未结算,未结算,""
     * 322,320,已结算,已结算,""
     *
     *
     * 430,0,分单主单对账状态,分单主单对账状态,XT_DIC_ACCOUNT_CHECK_STATUS
     * 431,430,未对账,未对账,
     * 432,430,已对账,已对账,

     */

    let loadStatus = {noLoad:351, partLoad:352, allLoad:353};

    let settlementStatus = {uncleared:321, cleared:322};

    let accountCheckStatus = {noCheck:431, checked:432};


    function accountStatusChange(accountStatus){
        let selectItems = grid.getSelecteds();
        if(selectItems.length==0){
            showAlert("请选择需要改变对账状态的分单!");
            return;
        }

        // 前端过滤，对已经完成配载且没有对账没有结算的分单进行对账状态修改
        // 需要注意的如果是已经结算过的分单是不能进行对账取消的
        let canAccountCheckIds = "";
        let canNotAccountCheckIds = "";

        if(accountStatus == accountCheckStatus.checked){
            for(let obj of selectItems){
                if(obj.billLoadStatusId != loadStatus.allLoad || obj.cancelStatusId == cancelStatus.canceled){
                    canNotAccountCheckIds += obj.id + " ";
                }
                else{
                    canAccountCheckIds += "'" + obj.id + "',";
                }
            }
        }
        else{
            for(let obj of selectItems){
                if(obj.settlementStatusId == settlementStatus.cleared){
                    canNotAccountCheckIds += obj.id + " ";
                }
                else{
                    canAccountCheckIds += "'" + obj.id + "',";
                }
            }
        }

        if(canNotAccountCheckIds.length>0){
            canNotAccountCheckIds = canNotAccountCheckIds.substring(0,canNotAccountCheckIds.length-1);
            if(accountStatus == accountCheckStatus.checked){
                showAlert("选择的对账分单【" + canNotAccountCheckIds + "】没有全部配载或已经作废，不能进行对账确认操作!");
            }
            else{
                showAlert("选择的对账分单【" + canNotAccountCheckIds + "】已结算，不能进行对账取消操作!");
            }
            return;
        }

        let title = accountStatus == accountCheckStatus.checked?"确认对账?":"取消对账?";
        canAccountCheckIds = canAccountCheckIds.substring(0,canAccountCheckIds.length-1);
        // 给出确认提示框
        mini.confirm("确定为分单【" + canAccountCheckIds + "】"+ title, "对账提示",
            function (action) {
                if (action == "ok") {
                    let ret = public_getServerData("${base}/pages/native_bill_manage/changeAccountCheckStatus",{billIds:canAccountCheckIds,accountCheckStatusId:accountStatus});
                    showAlert(ret.message,function (){
                        if(ret.ok){
                            grid.reload();
                        }
                    });
                }
            }
        );
    }



    /**
     * 设置底部统计样式
     * @param e
     */
    function summaryRight(e){
        e.cellStyle="text-align:right;color:red;font-size:18px;font-weight:bold";
    }


</script>

</body>
</html>